package com.yulintu.mq;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @author caomin
 * @date 2017/6/5
 * @说明 生产者
 */
@Component
public class FileSender {

    @Autowired
    private AmqpTemplate rabbitTemplate;

    /**
     * 将文件存放到mq中
     * @param fileList
     */
    public void sendMessage(List<File> fileList) {
        if (!CollectionUtils.isEmpty(fileList)){
            fileList.stream().forEach(file->{
                System.out.println("往mq存放文件======>>>>>>>>"+file.getName());
                rabbitTemplate.convertAndSend(FileMqConstans.FILE_EXCHANGE_NAME,"routeKey",file);
            });
        }
    }
    /**
     * 将文件存放到mq中
     * @param
     */
    public void sendMessage(File file) {
        System.out.println("mq发送文件:"+file.getName());
                rabbitTemplate.convertAndSend(FileMqConstans.FILE_EXCHANGE_NAME,"routeKey",file);
    }
    /**
     * 将文件存放到mq中
     * @param fileList
     */
    public void sendMessage(File[] fileList) throws IOException {
        if (fileList.length!=0){
            for (File file : fileList) {
                System.out.println("往mq存放文件======>>>>>>>>"+file.getName());
                rabbitTemplate.convertAndSend(FileMqConstans.FILE_EXCHANGE_NAME,"routeKey",file);
            }
        }
    }

}
